<?php
/*
Plugin Name: past-date
Plugin URI: http://wanwp.com
Description: 显示文章发表至今经过了多少时间
Author: 冰古
Version: 1.0
date: 200903271400
Author URI: http://bingu.net
License: GNU General Public License 2.0 http://www.gnu.org/licenses/gpl.html
*/
function past_date($prefix='发表至今：', $suffix='', $endtime='2419200'){

    $week = '周';
    $day = '天';
    $hour = '小时';
    $minute = '分钟';
    $second = '秒';

    // 自 PHP 5.1 起在 $_SERVER['REQUEST_TIME'] 中保存了发起该请求时刻的时间戳。
    if ($_SERVER['REQUEST_TIME'])
        $now_time = $_SERVER['REQUEST_TIME'];
    else
        $now_time = time();

    $m = 60;  // 一分钟
    $h = 3600;  //一小时有3600秒
    $d = 86400;  // 一天有86400秒
    $w = 604800;  // 一周

    $endtime = (int)$endtime;  // 结束时间

    $post_time = get_post_time('U', true);
    $past_time = $now_time - $post_time;  // 文章发表至今经过多少秒

    if($past_time > $endtime || $past_time <= 0){
        echo get_post_time('F jS, Y');
        return;
    }else if($past_time < $m){
        $past_date = $past_time . $second;
    }else if ($past_time < $h){
        $past_date = $past_time / $m;
        $past_date = floor($past_date);
        $past_date .= $minute;
    }else if (($past_time == $h) || ($past_time < ($h * 2))){
        $past_date = '1' . $hour;
        $past_date .= floor(($past_time % $h) / $m) . $minute;
    }else if ($past_time < $d){
        $past_date = $past_time / $h;
        $past_date = floor($past_date);
        $past_date .= $hour;
        $past_date .= floor(($past_time % $h) / $m) . $minute;
    }else if (($past_time == $d) || ($past_time < ($d * 2))){
        $past_date = '1' . $day;
        $past_date .= floor(($past_time % $d) / $h) . $hour;
    }else if ($past_time < $w){
        $past_date = $past_time / $d;
        $past_date = floor($past_date);
        $past_date .= $day;
        $past_date .= floor(($past_time % $d) / $h) . $hour;
    }else if(($past_time == $w) || ($past_time < ($w * 2))){
        $past_date = '1' . $week;
        $past_date .= floor(($past_time % $w) / $d) . $day;
    }else if ($past_time <= $endtime){
        $past_date = $past_time / $w;
        $past_date = floor($past_date);
        $past_date .= $week;
        $past_date .= floor(($past_time % $w) / $d) . $day;
    }

    $past_date = $prefix . $past_date . $suffix;
    echo $past_date;  // 打印文章发表至今经过多少时间
}